package org.Image.management.utils.remind;

import javax.swing.*;
import java.awt.*;
import java.util.function.Consumer;

//属性
public class ShowDialogImages {
    //新加提示框
    private final JDialog Dialog = new JDialog();
    //背景
    private Image background;
    //宽
    private int wide;
    //高
    private int high;
    //创建时间
    private long CreationTime;

    private ShowDialogImages() {
    }

    public ShowDialogImages(int wide, int high, Image background) {
        this.wide = wide;
        this.high = high;
        this.background = background;
    }

    /**
     * 关闭窗口
     *
     * @param consumer -> 关闭提示框的回调 返回值为 提示框显示时间 单位毫秒
     */
    public void ClosePrompt(Consumer<Long> consumer) {
        Dialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        Dialog.dispose();
        if (consumer != null) {
            consumer.accept(System.currentTimeMillis() - CreationTime);
        }
    }

    public void CloseAnimationPrompt(Consumer<Long> consumer) {

        // 降低 窗口透明度 关闭动画
        // 计算 延迟时间
        // for (int i = x; i >= 1; i--) {
        //     Dialog.setOpacity((i) * y);
        //     try {
        //         Thread.sleep(5);
        //     } catch (InterruptedException e) {
        //         System.out.println("关闭提示框异常" + e);
        //     }
        // }
        // setOpacity >= 1
        // i = x
        //
        // y =  1 / x
        try {
            for (int i = 25; i >= 1; i--) {
                Dialog.setOpacity((i) * 0.04f);
                Thread.sleep(6);
            }
        } catch (InterruptedException e) {
            System.out.println("关闭提示框异常" + e);
        }
        ClosePrompt(consumer);
    }
    // 线程关闭

    /***
     *  关闭提示框
     * @param Time -> 关闭提示框的时间
     * @param isOffAnimations -> 是否关闭动画 true 关闭 false 不关闭
     * @param consumer -> 关闭提示框的回调
     */
    public void CloseThread(int Time, boolean isOffAnimations, Consumer<Long> consumer) {
        int ClosePromptTime = Time <= 300 ? 1000 : Time;
        new Thread(() -> {
            try {
                Thread.sleep(ClosePromptTime);

                if (!isOffAnimations) {
                    // 降低 窗口透明度 关闭动画
                    for (int i = 25; i >= 1; i--) {
                        Dialog.setOpacity((i) * 0.04f);
                        Thread.sleep(6);
                    }
                }
                ClosePrompt(consumer);
            } catch (InterruptedException e) {
                System.out.println("关闭提示框异常" + e);
            }
        }).start();
    }


    public void show() {
        // 设置创建时间
        CreationTime = System.currentTimeMillis();

        //设置可调整大小
        Dialog.setResizable(false);
        //宽高
        Dialog.setSize(wide, high);
        //隐藏任务栏图标
        Dialog.setType(JFrame.Type.UTILITY);
        //置顶
        Dialog.setAlwaysOnTop(true);


        Dialog.setLocationRelativeTo(null);


        //设置是否不关闭就无法控制下面的面板
        Dialog.setModal(true);

        JPanel recordPanel = initPanel();
        //添加组件
        Dialog.add(recordPanel);

        //设置窗口的组件关闭
        Dialog.setUndecorated(true);
        //设置背景无颜色
        Dialog.setBackground(new Color(0, 0, 0, 0));

        //显示提示框
        Dialog.setVisible(true);
    }

    public JPanel initPanel() {
        //整个组件
        JPanel recordPanel = new JPanel();

        //设置背景
        if (this.background != null) {
            //背景
            JLabel background = new JLabel();
            //设置大小和位置
            background.setBounds(0, 0, wide, high);
            //文本居中
            background.setHorizontalAlignment(JLabel.CENTER);
            //设置背景
            background.setIcon(new ImageIcon(this.background));
            //文本框添加到提示框
            recordPanel.add(background);
        }


        //取消默认布局
        recordPanel.setLayout(null);

        //隐藏背景
        recordPanel.setOpaque(false);
        return recordPanel;
    }

}
